<template>
  <div class="addressList">
    <van-nav-bar
      title="选择地址"
      left-text="返回"
      left-arrow
      @click-left="back"
    />
    <van-address-list
      :list="list"
      v-model="chosenAddressId"
      default-tag-text="默认"
      @add="add"
      @edit="edit"
    />
  </div>
</template>

<script>
export default {
  name: "AddressList",

  data() {
    return {
      //地址列表数据
      list: [],
      chosenAddressId:''
    };
  },

  created() {
    this.getAddressListData();
  },

  methods: {
    back() {
      this.$router.go(-1);
    },

    //新增
    add() {
      this.$router.push({ name: "Address" });
    },

    //编辑
    edit({ aid }) {
      this.$router.push({ name: "Address", params: { aid } });
    },

    //查询用户地址列表
    getAddressListData() {
      let tokenString = this.$cookies.get("tokenString");

      if (!tokenString) {
        console.log("浏览器token不存在");
        return this.$router.push({ name: "Login" });
      }

      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        duration: 0,
      });

      this.axios({
        //请求方式
        method: "GET",
        url: this.baseUrl + "/findAddress",
        params: {
          appkey: this.appkey,
          tokenString,
        },
      })
        .then((res) => {
          // console.log("res getAddressListData ==> ", res);
          this.$toast.clear();

          if (res.data.code == 700) {
            this.$toast({
              message: res.data.msg,
              forbidClick: true,
              duration: 3000,
            });
            return this.$router.push({ name: "Login" });
          }

          if (res.data.code === 20000) {
            //添加address属性
            res.data.result.forEach((v) => {
              v.address = `${v.province}${v.city}${v.county}${v.addressDetail}`;
              v.isDefault = Boolean(v.isDefault);
            });

            this.list = res.data.result;

            // console.log('res.data.result ==> ', res.data.result);
          } else {
            this.$toast({
              message: res.data.msg,
              forbidClick: true,
              duration: 3000,
            });
          }
        })
        .catch((err) => {
          this.$toast.clear();
          console.log("err ==> ", err);
        });
    },
  },
};
</script>
